home *** CD-ROM | disk | FTP | other *** search
- //////////////////
- // String Class //
- //////////////////
-
- #include <string.h>
- #include <iostream.h>
-
- #include "string.hpp"
-
- ////////////////////
- // Create string. //
- ////////////////////
-
- String::String()
- {
- han = HeapAlloc (1); // allocate handle
- *(Address()) = 0; // store nul
- }
-
- String::String (char *s)
- {
- han = HeapAlloc (0); // allocate block
- Assign (s); // assign string
- }
-
- String::String (String& s)
- {
- han = HeapAlloc (0); // allocate block
- Assign (s); // assign string
- }
-
- ///////////////////////
- // Terminate string. //
- ///////////////////////
-
- String::~String ()
- {
- HeapFree (han); // release heap memory
- }
-
- ////////////////////////////
- // Assign a string value. //
- ////////////////////////////
-
- String& String::Assign (char * s)
- {
- unsigned l = strlen (s) + 1; // length of string
- HeapResize (han, l); // resize block
- CopyForward (HeapAddr (han), s, l); // copy data
- return *this;
- }
-
- String& String::Assign (String& s)
- {
- unsigned l = s.Length() + 1; // length of string
- HeapResize (han, l); // resize block
- CopyForward (HeapAddr(han), s.Address(), l); // copy data
- return *this;
- }
-
- ///////////////////////////////////
- // Insert a string to the front. //
- ///////////////////////////////////
-
- String& String::Prefix (char *s)
- {
- unsigned l1 = strlen(s); // bytes to add
- unsigned l2 = Length() + 1; // current bytes
- HeapResize (han, l1 + l2); // resize block
- char far *p = Address(); // current address
- CopyBackward (p + l1, p, l2); // make room for new string
- CopyForward (p, s, l1); // copy new string
- return *this;
- }
-
- String& String::Prefix (String& s)
- {
- unsigned l1 = s.Length(); // bytes to add
- unsigned l2 = Length() + 1; // current bytes
- HeapResize (han, l1 + l2); // resize block
- char far *p = Address(); // current address
- CopyBackward (p + l1, p, l2); // make room for new string
- CopyForward (p, s.Address(), l1); // copy new string
- return *this;
- }
-
- //////////////////////////////////
- // Insert a string to the back. //
- //////////////////////////////////
-
- String& String::Suffix (char *s)
- {
- unsigned l1 = strlen(s) + 1; // bytes to add
- unsigned l2 = Length(); // current bytes
- HeapResize (han, l1 + l2); // resize block
- CopyForward (Address() + l2, s, l1); // copy new string
- return *this;
- }
-
- String& String::Suffix (String& s)
- {
- unsigned l1 = s.Length() + 1; // bytes to add
- unsigned l2 = Length(); // current bytes
- HeapResize (han, l1 + l2); // resize block
- CopyForward (Address()+l2, s.Address(), l1); // copy new string
- return *this;
- }
-
- ////////////////////////////
- // Redirection functions. //
- ////////////////////////////
-
- char* operator<< (char* s1, String& s2)
- {
- CopyForward (s1, s2.Address(), s2.Length() + 1);
- return s1;
- }
-
- String& operator<< (String& s1, char* s2)
- {
- return s1.Assign (s2);
- }
-
- String& operator<< (String& s1, String& s2)
- {
- return s1.Assign (s2);
- }
-